package com.ruoyi.project.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.project.domain.BeamRealtime;
import com.ruoyi.project.service.IBeamRealtimeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.BeamRecord;
import com.ruoyi.project.service.IBeamRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 浆纱上机记录Controller
 *
 * @author ruoyi
 * @date 2022-01-02
 */
@RestController
@RequestMapping("/mes/jsrecord")
public class BeamRecordController extends BaseController {
    @Autowired
    private IBeamRecordService beamRecordService;
    @Autowired
    private IBeamRealtimeService beamRealtimeService;

    /**
     * 查询浆纱上机记录列表
     */
    @PreAuthorize("@ss.hasPermi('mes:jsrecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(BeamRecord beamRecord) {
        startPage();
        List<BeamRecord> list = beamRecordService.selectBeamRecordList(beamRecord);
        return getDataTable(list);
    }

    /**
     * 导出浆纱上机记录列表
     */
    @PreAuthorize("@ss.hasPermi('mes:jsrecord:export')")
    @Log(title = "浆纱上机记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BeamRecord beamRecord) {
        List<BeamRecord> list = beamRecordService.selectBeamRecordList(beamRecord);
        ExcelUtil<BeamRecord> util = new ExcelUtil<BeamRecord>(BeamRecord.class);
        return util.exportExcel(list, "浆纱上机记录数据");
    }

    /**
     * 获取浆纱上机记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('mes:jsrecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(beamRecordService.selectBeamRecordById(id));
    }

    /**
     * 新增浆纱上机记录
     */
    @PreAuthorize("@ss.hasPermi('mes:jsrecord:add')")
    @Log(title = "浆纱上机记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BeamRecord beamRecord) {
        BeamRealtime beamRealtime = beamRealtimeService.selectBeamRealtimeById(beamRecord.getMacid());
        beamRealtime.setStyleId(beamRecord.getId());
        beamRealtimeService.updateBeamRealtime(beamRealtime);

        return toAjax(beamRecordService.insertBeamRecord(beamRecord));
    }

    /**
     * 修改浆纱上机记录
     */
    @PreAuthorize("@ss.hasPermi('mes:jsrecord:edit')")
    @Log(title = "浆纱上机记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BeamRecord beamRecord) {
        return toAjax(beamRecordService.updateBeamRecord(beamRecord));
    }

    /**
     * 删除浆纱上机记录
     */
    @PreAuthorize("@ss.hasPermi('mes:jsrecord:remove')")
    @Log(title = "浆纱上机记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(beamRecordService.deleteBeamRecordByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('mes:jsrecord:remove')")
    @Log(title = "浆纱了机记录")
    @RequestMapping(value = "liaoji/{id}", method = RequestMethod.GET)
    public AjaxResult liaoji(@PathVariable Long id) {
        BeamRecord beamRecord = beamRecordService.selectBeamRecordById(id);
        BeamRealtime beamRealtime = beamRealtimeService.selectBeamRealtimeById(beamRecord.getMacid());
        beamRealtime.setStyleId(Long.valueOf(0));
        beamRecord.setSclength(beamRealtime.getBeamLength());
        beamRecord.setLjtime(new Date());
        beamRealtimeService.updateBeamRealtime(beamRealtime);
        beamRecordService.updateBeamRecord(beamRecord);

        return toAjax(1);


    }


}
